'use strict';

const Controller = require('egg').Controller;

class PostController extends Controller {
  async index() {
    const { ctx,app } = this;

    let tabs = [{
        name:"全部",
        url:"/admin/post",
        active:false
    },{
        name:"云领养",
        url:"?post_type=0",
        post_type:0,
        active:false
    },{
        name:"救助告示",
        url:"?post_type=1",
        post_type:1,
        active:false
    },{
        name:"萌宠日常",
        url:"?post_type=2",
        post_type:2,
        active:false
    }]


    tabs = tabs.map(item=>{
        if( (!ctx.query.post_type && ctx.query.post_type != 0 && item.url === '/admin/post') || item.post_type == ctx.query.post_type){
            item.active = true
        }
        return item
    })

    let where = (!ctx.query.post_type && ctx.query.post_type != 0) ? {} : {
        post_type:ctx.query.post_type
    }

    let data = await ctx.page('Post',where,{
        include:[{
            model:app.model.User,
            as:'pos'
        }]
    })

    data = JSON.parse(JSON.stringify(data))

    await ctx.renderTemplate({
        title:"帖子列表",
        tempType: "table",
        table:{
            tabs,
            // 表头
            columns:[{
                title:"id",
                fixed: 'center',
                width: 50,
                key:"id"
            },{
                title:"发帖人",
                fixed: 'left',
                width: 180,
                render(item) {
                    let image = item.pos.avatar ? item.pos.avatar : '/public/assets/img/profiles/avatar-03.jpg'
                    return `
                    <h2 class="table-avatar">
                         <a href="profile.html" class="avatar avatar-sm mr-2"><img class="avatar-img rounded-circle" src="${image}"></a>
                         <a href="profile.html">${item.pos.username}</a>
                    </h2>
                    `
                }
            },{
                title:"标题",
                fixed: 'center',
                width: 150,
                key:"title"
            },{
                title:"帖子类型",
                fixed: 'center',
                width: 100,
                render(item){
                    if(item.post_type == 0){
                        let typeName="云领养"
                        return `
                        ${typeName}
                        `
                    }
                    else if(item.post_type == 1){
                        let typeName="救助告示"
                        return `
                        ${typeName}
                        `
                    }
                    else if(item.post_type == 2){
                        let typeName="萌宠日常"
                        return `
                        ${typeName}
                        `
                    }
                }
            },{
                title:"最后一次修改时间",
                fixed: 'center',
                width: 180,
                key:"updated_time"
            },{
                title: "操作",
                width: 200,
                fixed: 'center',
                action:{
                    delete:function(id){
                        return `/admin/post/delete/${id}`
                    },
                }
            }]
        },
        data
    })
  }
  async delete(){
    const { ctx,app }=this;
    let id = ctx.params.id

    await app.model.Post.destroy({
        where:{
              id:id
        }
    })
    ctx.toast('删除成功','success')
    ctx.redirect('/admin/post')

  }
}

module.exports = PostController;
